<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
    >
      <el-form-item label="工作流名称" prop="workflowName">
        <el-input
          v-model="queryParams.workflowName"
          placeholder="请输入工作流名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['product:workflow:add']"
          >新增</el-button
        >
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['product:workflow:remove']"
          >删除</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
     stripe
    border
      v-loading="loading"
      :data="workflowList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" width="55" type="index" />
      <el-table-column
        label="工作流名称"
        align="center"
        prop="workflowName"
        min-width="150"
        show-overflow-tooltip
      />
      <el-table-column
        label="备注"
        align="center"
        prop="remark"
        min-width="150"
        show-overflow-tooltip
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
        width="200"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleSetting(scope.row)"
            v-hasPermi="['product:workflow:setting']"
            >设置步骤</el-button
          >
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['product:workflow:edit']"
            >修改</el-button
          >
          <!-- <el-button
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
            v-hasPermi="['product:workflow:remove']"
            >删除</el-button
          > -->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工作流对话框 -->
    <el-dialog v-dialogDrag :title="title" :visible.sync="open" width="700px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="96px">
        <el-form-item label="工作流名称" prop="workflowName">
          <el-input
            v-model="form.workflowName"
            placeholder="请输入工作流名称"
          />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input
            v-model="form.remark"
            placeholder="请输入备注"
            maxlength="100"
            show-word-limit
            rows="3"
            type="textarea"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="设置步骤"
      v-dialogDrag
      :close-on-click-modal="false"
      :visible.sync="showSetting"
      width="700px"
      append-to-body
    >
      <el-button
        type="primary"
        size="mini"
        style="
          display: flex;
          justify-content: flex-end;
          text-align: right;
          margin-bottom: 20px;
        "
        @click="addworkSteps"
        >增加工作流步骤</el-button
      >
      <el-timeline v-if="stepList.length > 0">
        <el-card>
          <el-timeline-item
            timestamp="审批角色"
            placement="top"
            v-for="(item, index) in stepList"
            :key="index"
          >
            <div class="line">
              <div>审核角色：</div>
              <div>
                <el-select
                  v-model="item.approverId"
                  @change="selectroleName($event, item)"
                >
                  <el-option
                    v-for="item in roleList"
                    :key="item.roleId"
                    :value="item.roleId"
                    :label="item.roleName"
                  ></el-option>
                </el-select>
              </div>
              <div style="margin-left: 150px">
                <el-button
                  type="primary"
                  size="mini"
                  @click="deleteSteps(index)"
                  >删除</el-button
                >
              </div>
            </div>

            <div class="line">
              <div>签字状态：</div>
              <div>
                <el-select v-model="item.signatureStatus">
                  <el-option
                    v-for="item in signatureStatusList"
                    :key="item.id"
                    :value="item.id"
                    :label="item.name"
                  ></el-option>
                </el-select>
              </div>
            </div>
          </el-timeline-item>
        </el-card>
      </el-timeline>
      <div slot="footer" class="dialog-footer" v-if="stepList.length > 0">
        <el-button type="primary" @click="submitFormStep">确 定</el-button>
        <el-button @click="showSetting = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  taskWorkflowStep,
  listWorkflow,
  getWorkflow,
  delWorkflow,
  addWorkflow,
  updateWorkflow,
} from "@/api/system/workflow";
import { listTaskWorkflowStep } from "@/api/system/taskWorkflowStep";
import { listRole } from "@/api/system/role";
export default {
  name: "Workflow",
  data() {
    return {
      roleList: [],
      signatureStatusList: [
        {
          id: 1,
          name: "校对",
        },
        {
          id: 2,
          name: "审核",
        },
      ],
      approverTypeList: [
        {
          id: 1,
          name: "人员",
        },
        {
          id: 2,
          name: "角色",
        },
      ],
      stepList: [],
      showSetting: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工作流表格数据
      workflowList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workflowName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        workflowName: [
          { required: true, message: "工作流名称不能为空", trigger: "blur" },
        ],
      },
      rowId: "",
    };
  },
  created() {
    this.getList();
    listRole().then((res) => {
      this.roleList = res.rows;
    });
  },
  methods: {
    selectroleName(e, item) {
      item.approverName = this.roleList.find(
        (item) => item.roleId == e
      ).roleName;
    },
    submitFormStep() {
      taskWorkflowStep(this.stepList).then((res) => {
        this.$modal.msgSuccess("设置成功");
        this.showSetting = false;
      });
    },
    //工作流删除按钮操作
    deleteSteps(index) {
      this.stepList.forEach((item, index1) => {
        if (index1 > index) {
          item.sequenceSteps = item.sequenceSteps - 1;
        }
      });
      this.stepList.splice(index, 1);
    },
    //增加工作流步骤的操作
    addworkSteps() {
      this.stepList.push({
        approverType: 2,
        signatureStatus: "",
        workflowType: this.rowId,
        sequenceSteps: this.stepList.length + 1,
      });
    },
    handleSetting(row) {
      listTaskWorkflowStep({
        workflowType:row.id
      }).then((res) => {
        res.rows.forEach(item=>{
          item.approverId = Number(item.approverId)
        })
        this.stepList = res.rows

      });
      this.rowId = row.id;
      this.showSetting = true;
    },
    /** 查询工作流列表 */
    getList() {
      this.loading = true;
      listWorkflow(this.queryParams).then((response) => {
        this.workflowList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        workflowName: null,
        remark: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工作流";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getWorkflow(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改工作流";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateWorkflow(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWorkflow(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除工作流数据项？")
        .then(function () {
          return delWorkflow(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "product/workflow/export",
        {
          ...this.queryParams,
        },
        `workflow_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
<style scoped lang="scss">
.line {
  display: flex;
  align-items: center;
  margin-top: 15px;
  > div:first-child {
    width: 90px;
  }
}
</style>
